<template>
  <el-config-provider :locale="zhCn">
    <router-view />
  </el-config-provider>
</template>

<script setup>
  import {
    provide
  } from 'vue'
  import { 
    ElMessage,
    ElMessageBox
  } from 'element-plus'
  import zhCn from 'element-plus/lib/locale/lang/zh-cn'
  import request from '/src/utils/request.js'
  import timeFormat from '/src/utils/timeFormat.js'
  import address from '/src/utils/address.js'
  import {
    useRouter,
    useRoute
  } from 'vue-router'
  import {
    useStore
  } from 'vuex'
  
  provide('$get', request.get);
  provide('$post', request.post);
  provide('$message', ElMessage);
  provide('$confirm', ElMessageBox.confirm);
  provide('$alert', ElMessageBox.alert);
  provide('$router', useRouter());
  provide('$route', useRoute());
  provide('$store', useStore());
  provide('$timeFormat', timeFormat);
  
  
  // 获取省
  provide('$getProvinces', () => {
    let provinces = address['86'];
    let result = [];
    for (let prop in provinces) {
      result.push({
        value: prop,
        label: provinces[prop]
      })
    }
    return result;
  });
  
  // 获取市
  provide('$getCities', code => {
    if(code == ''){
      throw new Error('code不能为空');
    }
    let cities = address[code];
    let result = [];
    for (let prop in cities) {
      result.push({
        value: prop,
        label: cities[prop]
      })
    }
    return result;
  });
  
  // 获取县
  provide('$getCountries', code => {
    if (code == '') {
      throw new Error('code不能为空');
    }
    let countries = address[code];
    let result = [];
    for (let prop in countries) {
      result.push({
        value: prop,
        label: countries[prop]
      })
    }
    return result;
  });
  
  // 上传
  provide('$upload', info => {
    return request.upload(info.action, {
      ...info.data,
      [info.filename]: info.file
    })
  });
  
  // 导出
  provide('$export', url => {
    location.href = `${import.meta.env.VITE_DOWNLOAD_URL}${url}`;
    ElMessage.success('导出成功');
  })
  
</script>

<style lang="scss">
  
  div.el-page-header__content {
    font-size: 14px;
  }
  
</style>
